package com.api.gbhg.liuwenbo.dao;

import com.api.gbhg.liuwenbo.api.NonCentralTreeApi;
import com.api.gbhg.liuwenbo.pojo.RealIncome;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class NonCentralTreeDBUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    private static PropertyManagementCostDao rmcd=new PropertyManagementCostDao();


    /**
     * 获取非中央所有分类信息
     * @return
     */
    public static Map<String,Object>  getSrysfl(){
        RecordSet rs = new RecordSet();
        String sql = " select id,mc from uf_srysfl ";
        rs.executeQuery(sql);
        Map<String,Object> map = new HashMap<>();
        while (rs.next()){
            map.put(rs.getString("id"),rs.getString("mc"));
        }
        return map;
    }

    /**
     * 根据id获取功能科目名称
     * @param id
     * @return
     */
    public static String getGnkmmcById(String id){
        RecordSet rs = new RecordSet();
        String sql =" select kmmc,kmdm from uf_gnkm where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("kmmc")+rs.getString("kmdm");
    }


    //-----------------------------------------------------------------------------------------------删除

    /**
     * 删除非中央项目级
     * @param param
     * @param uf_fzysrxm
     * @return
     */
    public static boolean defzysrxm(Map<String,Object> param,String uf_fzysrxm){
        String sjsrJe = getSjsrJeByXmid(param.get("id").toString());
        RecordSetTrans rs = new RecordSetTrans();
        rs.setAutoCommit(false);
        String id = NonCentralTreeApi.getfzyIdByxmId(param.get("id").toString());

        String xmid = param.get("id").toString();
        String yjsr = getCustomFieldsByXmid(xmid, "yjsr");
        String yqndyjjzje = getCustomFieldsByXmid(xmid, "yqndyjjzje");
        String yqndsjjzje = getCustomFieldsByXmid(xmid, "yqndsjjzje");

        String sql = " delete from "+uf_fzysrxm+" where id = "+param.get("id").toString()+"  ";

        try {
            LOGGER.info("非中央删除项目sql----："+sql);
            boolean dtIsSuccess = rs.executeUpdate(sql);
            if (dtIsSuccess){
                String dtSjsrSql = " delete uf_fzysjsr where szxm in (select id from uf_fzysrxm where szfl = "+param.get("id").toString()+") ";
                LOGGER.info("非中央删除收入后，删除实际收入sql----："+dtSjsrSql);
                if(!rs.executeUpdate(dtSjsrSql)){
                    rs.rollback();
                    return false;
                }

//                LOGGER.info("非中央查询实际收入sql----："+getSjsrSql);
//                rs.executeQuery(getSjsrSql);
//                String srje = "0";
//                if (rs.getCounts()>0){
//                    srje = rs.getString("srje");
//                }
                String summarySjsrSql = summarySjsrSql(id, sjsrJe);
                LOGGER.info("非中央收入同步实际收入sql----："+summarySjsrSql);
                if (!rs.executeUpdate(summarySjsrSql)){
                    rs.rollback();
                    return false;
                }

//                String sql2 = summaryLfSql(id, param); //删除后重新统计
                String sql2 = DeductionFlSql(id,yjsr,yqndyjjzje,yqndsjjzje,sjsrJe);
                LOGGER.info("非中央删除项目后重新统计sql----："+sql2);
                boolean ss = rs.executeUpdate(sql2);
                if (ss){
                    rs.commit();
                    return ss;
                }else{
                    rs.rollback();
                    return ss;
                }
            }else{
                rs.rollback();
                return dtIsSuccess;
            }
        } catch (Exception e) {
            rs.rollback();
            e.printStackTrace();
        }
        return false;
//        summaryLfSql
    }

//    /** up 21.12.1
//     * 删除非中央实际收入
//     * @param param
//     * @param uf_fzysjsr
//     * @return
//     */
//    public static boolean defzysjsr(Map<String,Object> param,String uf_fzysjsr){
//        String ids = param.get("id").toString();
//
//        String xmid = getXmIdBySjsrId(param.get("id").toString(), uf_fzysjsr);
//        String sjsrje = getSjsrJeByIds(param.get("id").toString());
//        String sjsr = getCustomFieldsByXmid(xmid,"sjsr");
//        String sjbxje = getCustomFieldsByXmid(xmid,"sjbxje");
//
//        BigDecimal bd1 = new BigDecimal(sjsr);
//        BigDecimal bd2 = new BigDecimal(sjsrje);
//        BigDecimal bd3 = new BigDecimal(sjbxje);
//        if (bd1.subtract(bd2).subtract(bd3).doubleValue() < 0){
//            return false;
//        }
//
//        RecordSetTrans rs = new RecordSetTrans();
//        rs.setAutoCommit(false);
//        boolean is = false;
//
//            String sql = " delete from "+uf_fzysjsr+" where id in ("+ids+")  ";
//            try {
//                LOGGER.info("非中央删除实际收入sql----："+sql);
//                boolean dtIsSuccess = rs.executeUpdate(sql);
//                String srid = NonCentralTreeApi.getfzyIdByxmId(xmid);
//                if (dtIsSuccess){
//                    String summarySrxmSql = " update uf_fzysrxm set sjsr = sjsr - "+sjsrje+",sjsrsy = sjsr - "+sjsrje+" - sjbxje " +
//                            " where id = "+xmid+"  ";
//                    LOGGER.info("非中央删除实际收入后，更新项目实际收入sql----："+summarySrxmSql);
//                    if(!rs.executeUpdate(summarySrxmSql)){
//                        rs.rollback();
//                        return false;
//                    }
//
//                    String xmsjsrje = getSjsrJe(srid);
//                    String summarySrSql = summarySjsrSql(srid, sjsrje);
//                    LOGGER.info("非中央删除实际收入后，更新收入实际收入金额sql----："+summarySrSql);
//                    if (!rs.executeUpdate(summarySrSql)){
//                        rs.rollback();
//                        return false;
//                    }
//                    is = true;
//                }else{
//                    rs.rollback();
//                    return dtIsSuccess;
//                }
//            } catch (Exception e) {
//                rs.rollback();
//                e.printStackTrace();
//            }
//        if (is){
//            rs.commit();
//            return true;
//        }else{
//            rs.rollback();
//            return false;
//        }
//    }

    /**
     * 删除非中央实际收入
     * @param param
     * @param uf_fzysjsr
     * @version 2.0
     * @date 21.12.1
     * @return
     */
    public static boolean defzysjsr(Map<String,Object> param,String uf_fzysjsr){
        String ids = param.get("id").toString();

        String xmid = getXmIdBySjsrId(param.get("id").toString(), uf_fzysjsr);
        String sjsrje = getSjsrJeByIds(param.get("id").toString());

        RecordSetTrans rs = new RecordSetTrans();
        rs.setAutoCommit(false);
        boolean is = false;

        String sql = " delete from "+uf_fzysjsr+" where id in ("+ids+")  ";
        try {
            LOGGER.info("非中央删除实际收入sql----："+sql);
            boolean dtIsSuccess = rs.executeUpdate(sql);
            String srid = NonCentralTreeApi.getfzyIdByxmId(xmid);
            if (dtIsSuccess){
                String summarySrxmSql = " update uf_fzysrxm set sjsr = sjsr - "+sjsrje+",sjsrsy = sjsrsy - "+sjsrje+" " +
                        " where id = "+xmid+"  ";
                LOGGER.info("非中央删除实际收入后，更新项目实际收入sql----："+summarySrxmSql);
                if(!rs.executeUpdate(summarySrxmSql)){
                    rs.rollback();
                    return false;
                }

                String summarySrSql = summarySjsrSql(srid, sjsrje);
                LOGGER.info("非中央删除实际收入后，更新收入实际收入金额sql----："+summarySrSql);
                if (!rs.executeUpdate(summarySrSql)){
                    rs.rollback();
                    return false;
                }
                is = true;
            }else{
                rs.rollback();
                return dtIsSuccess;
            }
        } catch (Exception e) {
            rs.rollback();
            e.printStackTrace();
        }
        if (is){
            rs.commit();
            return true;
        }else{
            rs.rollback();
            return false;
        }
    }

    /**
     * 根据项目id获取项目的实际收入
     * @param xmid
     * @return
     */
    public static String getCustomFieldsByXmid(String xmid,String fieldStr){
        RecordSet rs = new RecordSet();
        String sql2 = " select "+fieldStr+" from uf_fzysrxm where id = "+xmid+" ";
        rs.executeQuery(sql2);
        if(rs.next()) {
            return rs.getString(fieldStr);
        }else{
            return "未找到非财政项目NULL";
        }
    }

    /**
     * 根据id获取实际收入金额(可多个)
     * @param id
     * @return
     */
    public static String getSjsrJeByIds(String id){
        RecordSet rs = new RecordSet();
        String sql = " select SUM(srje) srje from uf_fzysjsr where id in("+id+") ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("srje");
    }

    /**
     * 根据实际收入id获取项目id
     * @param id
     * @param tabName
     * @return
     */
    public static String getXmIdBySjsrId(String id,String tabName){
        RecordSet rs = new RecordSet();
        String sql = " select szxm from "+tabName+" where id in ("+id+") ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szxm");
    }

    /**
     * 实际收入同步
     * @param srId
     * @param je
     * @return
     */
    public static String summarySjsrSql(String srId,String je){
        String sql = " update uf_fzysr set sjsr = sjsr - "+je+",sjsrsy = sjsrsy - "+je+" where id = "+srId+" ";
        return sql;
    }


    /**
     * 根据项目id获取实际收入金额
     * @param xmid
     * @return
     */
    public static String getSjsrJeByXmid(String xmid){
        RecordSet rs = new RecordSet();
        String sql = " select SUM(srje) srje from uf_fzysjsr where szxm = "+xmid+" ";
        rs.executeQuery(sql);
        rs.next();
        if (!rs.getString("srje").equals("")){
            return rs.getString("srje");
        }else{
            return "0";
        }
    }

    /**
     * 根据项目id获取实际收入金额
     * @param xmid
     * @return
     */
    public static String getSjsrsyJeByXmid(String xmid){
        RecordSet rs = new RecordSet();
        String sql = " select SUM(sjsrsy) sjsrsy from uf_fzysjsr where szxm = "+xmid+" ";
        rs.executeQuery(sql);
        rs.next();
        if (!rs.getString("sjsrsy").equals("")){
            return rs.getString("sjsrsy");
        }else{
            return "0";
        }
    }

    /**
     * 根据收入id查询收入下的项目实际金额
     * @param srId
     * @return
     */
    public static String getSjsrJe(String srId){
        RecordSet rs = new RecordSet();
        String sql = " select SUM(srje) srje from uf_fzysjsr where szxm in(select id from uf_fzysrxm where szfl = "+srId+") ";
        rs.executeQuery(sql);
        rs.next();
        if (!rs.getString("srje").equals("")){
            return rs.getString("srje");
        }else{
            return "0";
        }
    }

    public static String getsridByxmId(String id){
        RecordSet rs = new RecordSet();
        String sql = " select szfl as id from uf_fzysrxm where id = "+Integer.valueOf(id)+" ";
        rs.executeQuery(sql);
        rs.next();
        String flid = rs.getString("id");
        return flid;
    }

    /**
     * 删除非中央分类
     * @param param
     * @param uf_fzysr
     * @return
     */
    public static boolean defzysr(Map<String,Object> param,String uf_fzysr,String uf_fzysrxm){
        RecordSetTrans rs = new RecordSetTrans();
        rs.setAutoCommit(false);
        String sql = " delete from "+uf_fzysr+" where id = "+param.get("id").toString()+"  ";
        try {
            boolean dtissr = rs.executeUpdate(sql);
            LOGGER.info("非中央删除收入sql----："+sql);
            if (dtissr){
                String dtSjsrSql = " delete uf_fzysjsr where szxm in (select id from uf_fzysrxm where szfl = "+param.get("id").toString()+") ";
                LOGGER.info("非中央删除收入后，删除实际收入sql----："+dtSjsrSql);
                if(!rs.executeUpdate(dtSjsrSql)){
                    rs.rollback();
                    return false;
                }

                String dtxmSql = " delete from "+uf_fzysrxm+" where szfl = "+param.get("id").toString()+"  ";
                LOGGER.info("非中央删除收入后，删除下级项目sql----："+dtxmSql);
                boolean dtisxm = rs.executeUpdate(dtxmSql);
                if (dtisxm){
                    rs.commit();
                    return true;
                }else{
                    rs.rollback();
                    return false;
                }
            }else{
                rs.rollback();
                return false;
            }
        } catch (Exception e) {
            rs.rollback();
            e.printStackTrace();
        }
        return false;
    }

    //-----------------------------------------------------------------------------------------------修改

    /**
     * 修改项目信息
     * @param param
     * @param uf_fzysrxm
     * @return
     */
    public static boolean upfzysrxm(Map<String,Object> param,String uf_fzysrxm){
        param.remove("grade");

        String id = param.get("id").toString();
        param.remove("id");

        boolean yjsr = true;
        boolean yqndyjjzje = true;
        boolean yqndsjjzje = true;

        if (param.containsKey("yjsr")){
            yjsr = IsVcFzyxm(id, param.get("yjsr").toString(), "yhpje");
//            param.put("yjsrsy","yjsrsy - "+param.get("yjsr")+ " - yhpje ");
            param.put("yjsrsy",param.get("yjsr")+ " - yhpje ");
        }
        if (param.containsKey("yqndyjjzje")){
            yqndyjjzje = IsVcFzyxm(id, param.get("yqndyjjzje").toString(), "yqndyjjzyyje");
            param.put("yqndyjjzsyje",param.get("yqndyjjzje")+ " - yqndyjjzyyje");
        }
        if (param.containsKey("yqndsjjzje")){
            yqndsjjzje = IsVcFzyxm(id, param.get("yqndsjjzje").toString(), "yqndsjjzyyje");
//            param.put("yqndsjjzsyje","yqndsjjzsyje - "+param.get("yqndsjjzje"));
            param.put("yqndsjjzsyje",param.get("yqndsjjzje")+" - yqndsjjzyyje ");
        }


        if (yjsr && yqndyjjzje && yqndsjjzje){

            String[] name = param.keySet().toArray(new String[param.size()]);
            Object[] values = param.values().toArray();

            String sql = modifySql(uf_fzysrxm, name, values, id);
            RecordSetTrans rs = new RecordSetTrans();
            rs.setAutoCommit(false);


            try {
                boolean isup = rs.executeUpdate(sql);
                LOGGER.info("非中央修改项目sql----："+sql);
                if (isup){
                    String synQuerySql = " select SUM(yjsr) yjsr,SUM(yqndyjjzje) yjjzje,SUM(yqndsjjzje) sjjzje,szfl from uf_fzysrxm where szfl = (select szfl from uf_fzysrxm where id = "+id+") group by szfl ";
                    rs.executeQuery(synQuerySql);
                    rs.next();
                    String synSql = " update uf_fzysr set yjsr = "+rs.getString("yjsr")+" ,yqndyjjzje = "+rs.getString("yjjzje")+"," +
                            " yqndsjjzje = "+rs.getString("sjjzje")+" where id = "+rs.getString("szfl")+" ";
                    LOGGER.info("非中央修改时项目汇总sql----："+synSql);
                    if (rs.executeUpdate(synSql)){
                        rs.commit();
                        return true;
                    }else{
                        rs.rollback();
                        return false;
                    }
                }else{
                    rs.rollback();
                    return false;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            return false;
        }
        return false;
    }

    /**
     * 修改项目信息
     * @param param
     * @param uf_fzysrxm
     * @version 2.0
     * @date 21.12.1
     * @return
     */
    public static boolean upfzysrxm2(Map<String,Object> param,String uf_fzysrxm){
        param.remove("grade");

        String id = param.get("id").toString();
        param.remove("id");

        boolean yjsr = true;
        boolean yqndyjjzje = true;
        boolean yqndsjjzje = true;

        if (param.containsKey("yjsr")){
            yjsr = IsVcFzyxm(id, param.get("yjsr").toString(), "yhpje");
            String je = TreeStuctureDBUtil.getCustomSqlVle(uf_fzysrxm, "yjsr", id);
            param.put("yjsr"," yjsr +("+param.get("yjsr")+ " - "+je+" )");
            param.put("yjsrsy","yjsrsy+("+param.get("yjsr")+ " - "+je+") ");
        }
        if (param.containsKey("yqndyjjzje")){
            yqndyjjzje = IsVcFzyxm(id, param.get("yqndyjjzje").toString(), "yqndyjjzyyje");
            String je = TreeStuctureDBUtil.getCustomSqlVle(uf_fzysrxm, "yqndyjjzje", id);
            param.put("yqndyjjzsyje","yqndyjjzsyje+("+param.get("yqndyjjzje")+ " - "+je+")");
            param.put("yqndyjjzje","yqndyjjzje+("+param.get("yqndyjjzje")+ " - "+je+")");
        }
        if (param.containsKey("yqndsjjzje")){
            yqndsjjzje = IsVcFzyxm(id, param.get("yqndsjjzje").toString(), "yqndsjjzyyje");
            String je = TreeStuctureDBUtil.getCustomSqlVle(uf_fzysrxm, "yqndsjjzje", id);
            param.put("yqndsjjzsyje","yqndsjjzsyje+("+param.get("yqndsjjzje")+ " - "+je+")");
            param.put("yqndsjjzje","yqndsjjzje+("+param.get("yqndsjjzje")+ " - "+je+")");
        }

        if (yjsr && yqndyjjzje && yqndsjjzje){

            String[] name = param.keySet().toArray(new String[param.size()]);
            Object[] values = param.values().toArray();

            String sql = modifySql(uf_fzysrxm, name, values, id);
            RecordSetTrans rs = new RecordSetTrans();
            rs.setAutoCommit(false);


            try {
                boolean isup = rs.executeUpdate(sql);
                LOGGER.info("非中央修改项目sql----："+sql);
                if (isup){

                    String sltSql = "select szfl from "+uf_fzysrxm+" where id = "+id+" ";
                    rs.executeQuery(sltSql);
                    rs.next();

                    String synSql = " update uf_fzysr set " +
                            "yjsr = "+param.get("yjsr")+",yqndyjjzje = "+param.get("yqndyjjzje")+",yqndsjjzje = "+param.get("yqndsjjzje")+"," +
                            "yjsrsy = "+param.get("yjsrsy")+",yqndyjjzsyje = "+param.get("yqndyjjzsyje")+",yqndsjjzsyje = "+param.get("yqndsjjzsyje")+" " +
                            " where id = "+rs.getString("szfl")+" ";
                    LOGGER.info("修改非中央项目时 累加到非中央分类sql----："+synSql);
                    if (rs.executeUpdate(synSql)){
                        rs.commit();
                        return true;
                    }else{
                        rs.rollback();
                        return false;
                    }
                }else{
                    rs.rollback();
                    return false;
                }

            } catch (Exception e) {
                rs.rollback();
                e.printStackTrace();
            }
        }else{
            return false;
        }
        return false;
    }


    /**
     * 校验修改时剩余金额是否足够更改
     * @param id
     * @param je
     * @param str
     * @return
     */
    public static boolean IsVcFzy(String id,String je,String str){
        RecordSet rs = new RecordSet();
        String sql = " select "+str+" from uf_fzysr where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        BigDecimal bg1 = new BigDecimal(je);
        BigDecimal bg2 = new BigDecimal(rs.getString(str));
        /*-1,表示bd1小于bd2；
        0,表示bd1等于bd2；
        1,表示bd1大于bd2；*/
        int i = bg1.compareTo(bg2);
        if (i == 1 || i == 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 校验修改时剩余金额是否足够更改(项目表)
     * @param id
     * @param je
     * @param str
     * @return
     */
    public static boolean IsVcFzyxm(String id,String je,String str){
        RecordSet rs = new RecordSet();
//        String sql = " select yqndyjjzsyje from uf_fzysrxm where id = "+id+" ";
        String sql = " select "+str+" from uf_fzysrxm where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        BigDecimal bg1 = new BigDecimal(je);
        BigDecimal bg2 = new BigDecimal(rs.getString(str));//剩余金额
        /*-1,表示bd1小于bd2；
        0,表示bd1等于bd2；
        1,表示bd1大于bd2；*/
        int i = bg1.compareTo(bg2);
        if (i == 1 || i == 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 拼接修改同步sql
     * @param id
     * @return
     */
    public static boolean synchronizationSql(String id){
        RecordSet rs = new RecordSet();
        String sql = " select SUM(yjsr) yjsr,SUM(yqndyjjzje) yjjzje,SUM(yqndsjjzje) sjjzje,szfl from uf_fzysrxm where szfl = (select szfl from uf_fzysrxm where id = "+id+") group by szfl ";
        rs.executeQuery(sql);
        rs.next();
        String upsql = " update uf_fzysr set yjsr = "+rs.getString("yjsr")+" ,yqndyjjzje = "+rs.getString("yjjzje")+"," +
                " yqndsjjzje = "+rs.getString("sjjzje")+" where id = "+rs.getString("szfl")+" ";
        boolean isSuccess = rs.executeUpdate(upsql);
        return isSuccess;
    }

    /**
     * 修改分类信息
     * @param param
     * @param uf_fzysr
     * @return
     */
    public static boolean modifyFzysr(Map<String,Object> param,String uf_fzysr){
        param.remove("grade");
        String id = param.get("id").toString();
        param.remove("id");

        boolean yjsr = true;
        boolean yqndyjjzje = true;
        boolean yqndsjjzje = true;

        if (param.containsKey("yjsr")){
            yjsr = IsVcFzy(id, param.get("yjsr").toString(), "yhpje");
            param.put("yjsrsy",param.get("yjsr")+ " - yhpje ");
        }
        if (param.containsKey("yqndyjjzje")){
            yqndyjjzje = IsVcFzy(id, param.get("yqndyjjzje").toString(), "yqndyjjzyyje");
            param.put("yqndyjjzsyje",param.get("yqndyjjzje")+ " - yqndyjjzyyje");
        }
        if (param.containsKey("yqndsjjzje")){
            yqndsjjzje = IsVcFzy(id, param.get("yqndsjjzje").toString(), "yqndsjjzyyje");
            param.put("yqndsjjzsyje",param.get("yqndsjjzje")+" - yqndsjjzyyje ");
        }

        if (yjsr && yqndyjjzje && yqndsjjzje){
            String[] name = param.keySet().toArray(new String[param.size()]);
            Object[] values = param.values().toArray();

            String sql = modifySql(uf_fzysr, name, values, id);
            RecordSet rs = new RecordSet();
            LOGGER.info("非中央修改收入sql----："+sql);
            return rs.executeUpdate(sql);
        }else{
            return false;
        }
    }

    /**
     * 修改分类信息
     * @param param
     * @param uf_fzysr
     * @version 2.0
     * @date 21.12.1
     * @return
     */
    public static boolean modifyFzysr2(Map<String,Object> param,String uf_fzysr){
        param.remove("grade");
        String id = param.get("id").toString();
        param.remove("id");

        param.remove("yjsr");
        param.remove("yjsrsy");
        param.remove("yqndyjjzje");
        param.remove("yqndyjjzsyje");
        param.remove("yqndsjjzje");
        param.remove("yqndsjjzsyje");
        param.remove("sjsr");

        String[] name = param.keySet().toArray(new String[param.size()]);
        Object[] values = param.values().toArray();

        String sql = modifySql(uf_fzysr, name, values, id);
        RecordSet rs = new RecordSet();
        LOGGER.info("非中央修改收入sql----："+sql);
        return rs.executeUpdate(sql);
    }

    /**
     * 拼接修改sql
     * @param tbName
     * @param name
     * @param values
     * @param id
     * @return
     */
    public static String modifySql(String tbName, String[] name, Object[] values, String id){
        StringBuffer sb = new StringBuffer();
        sb.append(" update "+tbName+" set ");
        for (int i = 0; i < name.length; i++) {
            if (values[i].equals(""))
                continue;
            if (name[i].equals("xmmc") || name[i].equals("sfwdlhsdw") || name[i].equals("srzx") || name[i].equals("xmdm") || name[i].equals("glh")){

                sb.append(""+name[i]+" = '"+values[i]+"',");
            }else if(name[i].equals("gnkm")){
                sb.append(""+name[i]+" = '"+values[i]+"',");
            }else{
                sb.append(""+name[i]+" = "+values[i]+",");
            }
        }
        return sb.substring(0,sb.length() - 1).toString() + " where id = "+id+" ";
    }

    //----------------------------------------------------------------------------------------------------------------查询实际收入列表

    /**
     * 根据所在项目id查询实际收入
     * @param tabName
     * @param id
     * @return
     */
    public static List<RealIncome> getSjsrList(String tabName, String id){
        RecordSet rs = new RecordSet();
        String sql = " select * from  "+tabName+" where szxm = "+id+" ";
        rs.executeQuery(sql);
        List<RealIncome> list = new ArrayList<>();
        while (rs.next()){
            RealIncome tree = new RealIncome();
            tree.setId(rs.getString("id"));
            tree.setYsnf(rs.getString("nf"));

            int dwid = rs.getInt("szdw");
            RecordSet sbdwById = rmcd.getSBDWById(dwid);
            RecordSet dw = rmcd.getSBDWById(dwid);
            dw.next();
            String dwStr = dw.getString("SUBCOMPANYNAME");
            tree.setSzdw(dwStr);

            String yjsrfl = NonCentralTreeApi.getFlNameById(rs.getString("szfl"));
            tree.setSzfl(yjsrfl);
            tree.setGnkm(rs.getString("gnkmmc"));
            tree.setSfczbk(rs.getString("sfczbk"));
            tree.setSrsx(rs.getString("srsx"));
            tree.setLydw(rs.getString("lydw"));
            tree.setSrrq(rs.getString("srrq"));
            tree.setSrje(rs.getString("srje"));
            String xmmc = getxmmcById(rs.getString("szxm"));
            tree.setName(xmmc);
            list.add(tree);
        }
        return list;
    }

    /**
     * 根据项目id取项目名称
     * @param id
     * @return
     */
    public static String getxmmcById(String id){
        RecordSet rs = new RecordSet();
        String sql = " select xmmc from uf_fzysrxm where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("xmmc");
    }

    //--------------------------------------------------------------------------------------------------------------------------新增

    /**
     * 添加非中央分类
     * @param param
     * @param uf_fzysr
     * @return
     */
    public static boolean addNonCt(Map<String,Object> param,String uf_fzysr){
        param.remove("grade");

        String id = Isfzysr(param,uf_fzysr);
        if (!id.isEmpty()){
            boolean isSuccess = upfzysr(param, uf_fzysr, id);
            return isSuccess;
        }else{
            String uuid = UUID.randomUUID().toString();
            Integer modeId = DataBaseUtil.getModeId(uf_fzysr);
            List<String> list = new ArrayList<>();
            String[] names =  param.keySet().toArray(new String[param.size()]);
            Object[] values = param.values().toArray();

            String modeInsertSql = DataBaseUtil.getModeInsertSql(Arrays.asList(names), uf_fzysr, uuid, modeId);
            RecordSet rs = new RecordSet();
            boolean IsSuccess = rs.executeUpdate(modeInsertSql, values);
            LOGGER.info("非中央新增类别sql----："+modeInsertSql+"+-value:+"+values);
            return IsSuccess;
        }
    }

    /**
     * 更新非中央类别信息
     * @param param
     * @param uf_fzysr
     * @return
     */
    public static boolean upfzysr(Map<String,Object> param,String uf_fzysr,String id){
        RecordSet rs = new RecordSet();
        String[] names = param.keySet().toArray(new String[param.size()]);
        Object[] values = param.values().toArray();

        String sql = upfzysrSql(uf_fzysr, names, values, id);
        boolean isSuccess = rs.executeUpdate(sql);
        return isSuccess;
    }

    /**
     * 拼接更新非中央类别sql
     * @param tbName
     * @param name
     * @param values
     * @param id
     * @return
     */
    public static String upfzysrSql(String tbName, String[] name, Object[] values, String id){
        StringBuffer sb = new StringBuffer();
        sb.append(" update "+tbName+" set ");
        for (int i = 0; i < name.length; i++) {
            if (values[i].equals(""))
                continue;

            sb.append(" "+name[i]+" = "+values[i]+",");
        }
        return sb.substring(0,sb.length() - 1).toString() + " where id = "+id+" ";
    }

    /**
     * 校验类别是否存在
     * @param param
     * @param uf_fzysr
     * @return
     */
    public static String Isfzysr(Map<String,Object> param,String uf_fzysr){
        RecordSet rs = new RecordSet();
        String sql = " select * from "+uf_fzysr+" where nf = '"+param.get("nf").toString()+"' and dw = '"+param.get("dw").toString()+"' and szfl = '"+param.get("szfl").toString()+"' ";
        rs.executeQuery(sql);
        while (rs.next()){
            return rs.getString("id");
        }
        return "";
//        if (rs.getCounts()>0){
//            return true;
//        }else{
//            return false;
//        }
    }

    public static boolean addNonCtXm(Map<String,Object> param,String uf_fzysrxm){
        if (param.get("szfl").toString().equals(""))
            return false;
        param.remove("grade");
        String uuid = UUID.randomUUID().toString();
        Integer modeId = DataBaseUtil.getModeId(uf_fzysrxm);

        String[] names =  param.keySet().toArray(new String[param.size()]);
        Object[] values = param.values().toArray();


        String modeInsertSql = DataBaseUtil.getModeInsertSql(Arrays.asList(names), uf_fzysrxm, uuid, modeId);
        RecordSetTrans rs = new RecordSetTrans();
        rs.setAutoCommit(false);
        try {
            boolean IsSuccess = rs.executeUpdate(modeInsertSql, values);
            LOGGER.info("非中央新增项目sql----："+modeInsertSql+"+-value:+"+values);
            boolean up = false;
            if (IsSuccess){
//            String sql = AddxjSql(param.get("szfl").toString(), param);
                //8跟5为地方财政项目支出跟其他收入 新增项目时为求和方式同步上级
                if (param.get("yssrfl").equals("8") || param.get("yssrfl").equals("5")){
                    String sql = summaryLfSql(param.get("szfl").toString(), param);
                    up = rs.executeUpdate(sql);
                    LOGGER.info("非中央项目汇总到类别sql--求和--："+sql);
                }else{
                    String sql = summaryLfSql2(param.get("szfl").toString(), param.get("yjsr").toString(),
                            param.get("yqndyjjzje").toString(), param.get("yqndsjjzje").toString());
                    up = rs.executeUpdate(sql);
                    LOGGER.info("非中央项目汇总到类别sql--累加--："+sql);
                }

                if (up){
                    rs.commit();
                    return true;
                }else{
                    rs.rollback();
                    return false;
                }

            }else{
                rs.rollback();
                return false;
            }
        } catch (Exception e) {
            rs.rollback();
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 项目汇总到类别sql 求和方式
     * @param id
     * @param param
     * @return
     */
    public static String summaryLfSql(String id,Map<String,Object> param){
        RecordSet rs = new RecordSet();
        String sql = " select sum(yjsr) yjsr,sum(yqndyjjzje) yqndyjjzje,sum(yqndsjjzje) yqndsjjzje " +
                " ,sum(yjsrsy) yjsrsy,sum(yqndyjjzsyje) yqndyjjzsyje,sum(yqndsjjzsyje) yqndsjjzsyje  from uf_fzysrxm where szfl = "+id+" ";
        rs.executeQuery(sql);

        String yjsr = "0";
        String yqndyjjzje = "0";
        String yqndsjjzje = "0";
        String yqndyjjzsyje = "0";
        String yqndsjjzsyje = "0";
        rs.next();
        if (!rs.getString("yjsr").equals("")){
            yjsr = rs.getString("yjsr");
            yqndyjjzje = rs.getString("yqndyjjzje");
            yqndsjjzje = rs.getString("yqndsjjzje");
            yqndyjjzsyje = rs.getString("yqndyjjzsyje");
            yqndsjjzsyje = rs.getString("yqndsjjzsyje");
            String upSql = " update uf_fzysr set yjsr = "+yjsr+",yqndyjjzje = "+yqndyjjzje+",yqndsjjzje = "+yqndsjjzje+"," +
                    " yjsrsy = "+yjsr+",yqndyjjzsyje = "+yqndyjjzsyje+",yqndsjjzsyje = "+yqndsjjzsyje+" " +
                    "  where id = "+id+" ";
            return upSql;
        }else{
            String upSql = " update uf_fzysr set yjsr = "+yjsr+",yqndyjjzje = "+yqndyjjzje+",yqndsjjzje = "+yqndsjjzje+"," +
                    " yjsrsy = "+yjsr+",yqndyjjzsyje = "+yqndyjjzsyje+",yqndsjjzsyje = "+yqndsjjzsyje+" " +
                    " where id = "+id+" ";
            return upSql;
        }
    }


    /**
     * 非中央预算分类扣减sql
     * @param id
     * @param yjsrJe
     * @param yqndyjjzje
     * @param yqndsjjzje
     * @param sjsrje
     * @return
     */
    public static String DeductionFlSql(String id,String yjsrJe,String yqndyjjzje,String yqndsjjzje,String sjsrje){
        String upSql = " update uf_fzysr set yjsr = yjsr - "+yjsrJe+",yqndyjjzje = yqndyjjzje - "+yqndyjjzje+",yqndsjjzje = yqndsjjzje - "+yqndsjjzje+"," +
                " yjsrsy = yjsrsy - "+yjsrJe+",yqndyjjzsyje = yqndyjjzsyje - "+yqndyjjzje+",yqndsjjzsyje = yqndsjjzsyje - "+yqndsjjzje+"," +
                "sjsr = sjsr - "+sjsrje+",sjsrsy = sjsrsy - "+sjsrje+" " +
                " where id = "+id+" ";
        return upSql;
    }

    /**
     * 项目汇总到类别sql 累加方式
     * @param id
     * @param pYjsr
     * @param pYqndyjjzje
     * @param pYqndsjjzje
     * @return
     */
    public static String summaryLfSql2(String id,String pYjsr,String pYqndyjjzje,String pYqndsjjzje){
        String sql = " select sum(yjsr) yjsr,sum(yqndyjjzje) yqndyjjzje,sum(yqndsjjzje) yqndsjjzje from uf_fzysrxm where szfl = "+id+" ";

        String yjsr = pYjsr.equals("")?"0":pYjsr;
        String yqndyjjzje = pYqndyjjzje.equals("")?"0":pYqndyjjzje;
        String yqndsjjzje = pYqndsjjzje.equals("")?"0":pYqndsjjzje;

        String upSql = " update uf_fzysr set yjsr = yjsr+"+yjsr+",yqndyjjzje = yqndyjjzje+"+yqndyjjzje+",yqndsjjzje = yqndsjjzje+"+yqndsjjzje+"," +
                " yjsrsy = yjsrsy+"+yjsr+",yqndyjjzsyje = yqndyjjzsyje+"+yqndyjjzje+",yqndsjjzsyje = yqndsjjzsyje+"+yqndsjjzje+" " +
                "where id = "+id+" ";
        return upSql;
    }

    public static String AddxjSql(String Id,Map<String,Object> param){

        StringBuffer sb = new StringBuffer();
        sb.append(" update uf_fzysr set ");
        if (param.containsKey("yjsr"))
            sb.append("yjsr = yjsr+'"+param.get("yjsr")+"',");
        if (param.containsKey("sjsr"))
            sb.append("sjsr = sjsr+"+param.get("sjsr")+",");
        if (param.containsKey("yqndyjjzje"))
            sb.append("yqndyjjzje = yqndyjjzje+"+param.get("yqndyjjzje")+",");
        if (param.containsKey("yqndsjjzje"))
            sb.append("yqndsjjzje = yqndsjjzje+"+param.get("yqndsjjzje")+",");

        return sb.substring(0,sb.length() - 1).toString() + " where id = "+Id+" ";
    }

    /**
     * 根据id获取分类id
     * @param id
     * @return
     */
    public static String getLbidById(String id){
        RecordSet rs = new RecordSet();
        String sql = " select szfl from uf_fzysr where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szfl");
    }

    /**
     * 根据项目id获取分类id
     * @param id
     * @return
     */
    public static String getLbidByxmId(String id){
        RecordSet rs = new RecordSet();
        String sql = " select z.szfl from uf_fzysr z left join uf_fzysrxm c on z.id=c.szfl where c.id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szfl");
    }

}
